local assets =
{
    Asset("ANIM", "anim/yuemiheng_equipment.zip"),
	Asset("ATLAS", "images/inventoryimages/yuemiheng_tool.xml"), 
    Asset("IMAGE", "images/inventoryimages/yuemiheng_tool.tex"),
}

local prefabs =
{
    "yuemiheng_equipment_fx",
}

local function SetFxOwner(inst, owner)
    if inst._fxowner ~= nil and inst._fxowner.components.colouradder ~= nil then
        inst._fxowner.components.colouradder:DetachChild(inst.fx)
    end
    inst._fxowner = owner
    if owner ~= nil then
        inst.fx.entity:SetParent(owner.entity)
        inst.fx.Follower:FollowSymbol(owner.GUID, "swap_object", nil, nil, nil, true)
        inst.fx.components.highlightchild:SetOwner(owner)
        if owner.components.colouradder ~= nil then
            owner.components.colouradder:AttachChild(inst.fx)
        end
    else
        inst.fx.entity:SetParent(inst.entity)
        --For floating
        inst.fx.Follower:FollowSymbol(inst.GUID, "swap_spear", nil, nil, nil, true)
        inst.fx.components.highlightchild:SetOwner(inst)
    end
end

local function PushIdleLoop(inst)
    if inst.components.finiteuses:GetUses() > 0 then
        inst.AnimState:PushAnimation("idle")
    end
end

local function OnStopFloating(inst)
    if inst.components.finiteuses then
        if inst.components.finiteuses:GetUses() > 0 then
            inst.fx.AnimState:SetFrame(0)
            inst:DoTaskInTime(0, PushIdleLoop) --#V2C: #HACK restore the looping anim, timing issues
        end
    end
    
end

local function onequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_object")
    owner.AnimState:Show('ARM_carry')
    owner.AnimState:Hide('ARM_normal')


    SetFxOwner(inst, owner)
end

local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_object")
    owner.AnimState:Hide('ARM_carry')
    owner.AnimState:Show('ARM_normal')


    SetFxOwner(inst, nil)
end

local function SetupComponents(inst)
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    -- inst:AddComponent("weapon")
    -- inst.components.weapon:SetDamage(0)
    -- inst.components.weapon:SetRange(8, 10)
end

local function onfinished(inst)
    inst:Remove()
end

local function onsave(inst,data)
    if inst:HasTag('ymh_tool_removed_finiteuse') then
        data.ymh_tool_removed_finiteuse = true
    end
end

local function onpreload(inst,data)
    if data and data.ymh_tool_removed_finiteuse then
        if not inst:HasTag('ymh_tool_removed_finiteuse') then
            inst:AddTag('ymh_tool_removed_finiteuse')
        end
        if inst.components.finiteuses then
            inst:RemoveComponent('finiteuses')
        end
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("yuemiheng_tool")
    inst.AnimState:SetBuild("yuemiheng_equipment")
    inst.AnimState:PlayAnimation("idle", true)
    inst.AnimState:SetSymbolBloom("pb_energy_loop")
    inst.AnimState:SetSymbolBloom("stone")
    inst.AnimState:SetSymbolLightOverride("pb_energy_loop01", .5)
    inst.AnimState:SetSymbolLightOverride("pb_ray", .5)
    inst.AnimState:SetSymbolLightOverride("stone", .5)
    inst.AnimState:SetSymbolLightOverride("glow", .25)
    inst.AnimState:SetLightOverride(.1)

    inst:AddTag("rangedweapon")
    inst:AddTag("magicweapon")
    inst:AddTag("show_broken_ui")

    --weapon (from weapon component) added to pristine state for optimization
    inst:AddTag("weapon")

    inst:AddTag("nopunch")

    inst.projectiledelay = FRAMES

    inst:AddComponent("floater")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then

        return inst
    end

    local frame = 1
    inst.AnimState:SetFrame(frame)
    inst.fx = SpawnPrefab("yuemiheng_equipment_fx")
    inst.fx.AnimState:SetFrame(frame)
    SetFxOwner(inst, nil)
    inst:ListenForEvent("floater_stopfloating", OnStopFloating)

    inst:AddComponent("inspectable") --可悲检查组件

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/yuemiheng_tool.xml" 

    inst:AddComponent("tool")
    inst.components.tool:SetAction(ACTIONS.CHOP, 3)
    inst.components.tool:SetAction(ACTIONS.MINE, 2)
    inst.components.tool:SetAction(ACTIONS.DIG, 1)
    inst.components.tool:EnableToughWork(true)

    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(TUNING.MOD_YUEMIHENG.TOOL.FINITEUSE)
    inst.components.finiteuses:SetUses(TUNING.MOD_YUEMIHENG.TOOL.FINITEUSE)
    inst.components.finiteuses:SetOnFinished(onfinished)
    inst.components.finiteuses:SetConsumption(ACTIONS.CHOP,1)
    inst.components.finiteuses:SetConsumption(ACTIONS.MINE,1)

    inst:AddComponent('named')

    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")

    SetupComponents(inst)

    local setbonus = inst:AddComponent("setbonus")
    setbonus:SetSetName(EQUIPMENTSETNAMES.LUNARPLANT)


    inst.noplanarhitfx = true

    inst.OnSave = onsave
    inst.OnPreLoad = onpreload

    return inst
end

local function fxfn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddFollower()
    inst.entity:AddNetwork()

    inst:AddTag("FX")

    inst.AnimState:SetBank("yuemiheng_tool")
    inst.AnimState:SetBuild("yuemiheng_equipment")
    inst.AnimState:PlayAnimation("swap_loop", true)
    inst.AnimState:SetSymbolBloom("pb_energy_loop")
    inst.AnimState:SetSymbolBloom("stone")
    inst.AnimState:SetSymbolLightOverride("pb_energy_loop01", .5)
    inst.AnimState:SetSymbolLightOverride("pb_ray", .5)
    inst.AnimState:SetSymbolLightOverride("stone", .5)
    inst.AnimState:SetSymbolLightOverride("glow", .25)
    inst.AnimState:SetLightOverride(.1)

    inst:AddComponent("highlightchild")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("colouradder")

    inst.persists = false

    return inst
end

return Prefab("yuemiheng_tool", fn, assets, prefabs),
    Prefab("yuemiheng_equipment_fx", fxfn, assets)